<template>
  <el-form :model="form" :rules="rules" ref="form" label-width="80px">
    <el-form-item label="邮箱" prop="username">
      <el-input type="text" v-model="form.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="form.password" type="password"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass" label="确认密码">
      <el-input type="password" v-model="form.checkPass"></el-input>
    </el-form-item>
    <el-form-item label="昵称">
      <el-input v-model="form.nickname"></el-input>
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="form.gender">
        <el-radio label="m">男</el-radio>
        <el-radio label="f">女</el-radio>
        <el-radio label="o">其他</el-radio>
        <el-radio lebel="s">保密</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="自我介绍">
      <el-input type="textarea" v-model="form.description"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('form')">注册</el-button>
      <el-button type="text">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'RegisterForm',
  data () {
    const validPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码必须一致'))
      } else {
        callback()
      }
    }
    const validUsername = (rule, value, callback) => {
      this.$axios.get('check_user?username=' + value).then(resp => {
        if (resp.data.payload === 0) {
          callback(new Error('此邮箱地址已注册过，更换其他邮箱地址'))
        } else {
          callback()
        }
      })
    }
    return {
      rules: {
        username: [
          {
            required: true,
            message: '请输入你的邮箱地址',
            trigger: 'blur'
          },
          {
            type: 'email',
            message: '邮箱格式不正确',
            trigger: 'blur'
          },
          {
            max: 200,
            message: '输入内容过长',
            trigger: 'change'
          }, {
            validator: validUsername,
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 32,
            message: '密码必须大于6位小于32位',
            trigger: 'change'
          }
        ],
        checkPass: [{
          required: true,
          validator: validPass,
          trigger: 'change'
        }]
      },
      allow: false,
      form: {
        username: '',
        password: '',
        checkPass: '',
        nickname: '',
        gender: 's',
        description: ''
      }
    }
  },
  methods: {
    onSubmit (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios.post('register', this.form).then(resp => {
            if (resp.data.payload === 'success') {
              this.$message('注册成功，即将转至登录页面')
              // this.$router.replace('/login')
            }
          })
        } else {
          this.$message('检查你的输入')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
